package com.cdd.eduservice.controller;


import com.cdd.commonutils.R;
import com.cdd.eduservice.client.VodClient;
import com.cdd.eduservice.entity.EduVideo;
import com.cdd.eduservice.service.IEduVideoService;
import com.cdd.servicebase.handler.SmartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author cdd
 * @since 2021-02-02
 */
@RestController
@RequestMapping("/eduservice/edu-video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private IEduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;

    // 添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        Date date = new Date();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        eduVideo.setGmtCreate(localDateTime);
        eduVideo.setGmtModified(localDateTime);
        boolean save = eduVideoService.save(eduVideo);
        return R.ok();
    }

    // 删除小节
    @DeleteMapping("{videoId}")
    public R deleteVideo(@PathVariable String videoId) {
        EduVideo eduVideo = eduVideoService.getById(videoId);
        String vodId = eduVideo.getVideoSourceId();
        if(!StringUtils.isEmpty(vodId)) {
            R r = vodClient.deleteVod(vodId);
            if(r.getCode() == 20001) {
                throw new SmartException(20001,"删除视频失败，熔断器...");

            }
        }
        eduVideoService.removeById(videoId);
        return R.ok();
    }

    // 修改小节 TODO

}

